package cn.hashq.netpoststation.service.impl;

import cn.hashq.netpoststation.dto.AddClientDTO;
import cn.hashq.netpoststation.entity.Client;
import cn.hashq.netpoststation.entity.PortMap;
import cn.hashq.netpoststation.mapper.ClientMapper;
import cn.hashq.netpoststation.mapper.PortMapMapper;
import cn.hashq.netpoststation.service.ClientManagerService;
import cn.hashq.netpoststation.session.SessionMap;
import cn.hashq.netpoststation.vo.ServerResponse;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ClientManagerServiceImpl extends ServiceImpl<ClientMapper, Client> implements ClientManagerService {

    @Resource
    private PortMapMapper portMapMapper;

    @Override
    public ServerResponse addClient(AddClientDTO clientDTO) {
        if (StrUtil.isBlank(clientDTO.getName()) || StrUtil.isBlank(clientDTO.getPassword())) {
            return ServerResponse.createFailedResult("参数不能为空");
        }
        Client client = new Client();
        client.setSecret(clientDTO.getPassword());
        client.setClientName(clientDTO.getName());
        client.setStatus(1);
        save(client);
        return ServerResponse.createSuccessResult();
    }

    @Override
    public ServerResponse listClient() {
        List<Client> list = list();
        return ServerResponse.createSuccessResult(list);
    }

    @Override
    public ServerResponse removeClient(Long id) {
        LambdaQueryWrapper<PortMap> queryWrapper = Wrappers.<PortMap>lambdaQuery()
                .select(PortMap::getServerPort)
                .eq(PortMap::getClientId, id);
        List<Integer> ports = portMapMapper.selectList(queryWrapper)
                .stream().map(PortMap::getServerPort).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(ports)) {
            return ServerResponse.createFailedResult("请删除当前客户端下TCP隧道后,删除客户端");
        }
        if (Objects.nonNull(SessionMap.inst().getSession(id))) {
            SessionMap.inst().getSession(id).close();
            SessionMap.inst().removeSession(id);
        }
        removeById(id);
        return ServerResponse.createSuccessResult();
    }

    @Override
    public ServerResponse getClientById(Long id) {
        Client client = getById(id);
        return ServerResponse.createSuccessResult(client);
    }

    @Override
    public ServerResponse notifyClientStatus(Long id) {
        if (Objects.isNull(id)) {
            return ServerResponse.createFailedResult("id不能为空");
        }
        Client client = getById(id);
        if ((client.getStatus().compareTo(1) == 0)) {
            // 关闭客户端连接
            client.setStatus(0);
            SessionMap.inst().getSession(id).close();
            SessionMap.inst().removeSession(id);
        } else {
            client.setStatus(1);
        }
        updateById(client);
        return ServerResponse.createSuccessResult();
    }

    @Override
    public ServerResponse updateClient(Client client) {
        if (Objects.isNull(client.getClientId())) {
            return ServerResponse.createFailedResult("id不能为空");
        }
        if (StrUtil.isBlank(client.getSecret())) {
            return ServerResponse.createFailedResult("密钥不能为空");
        }
        if (client.getStatus().compareTo(0) == 0) {
            SessionMap.inst().getSession(client.getClientId()).close();
            SessionMap.inst().removeSession(client.getClientId());
        }
        updateById(client);
        return ServerResponse.createSuccessResult();
    }
}
